package hn.cch.spring.aop.proxy.proxy;


import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * 动态代理测试
 */
public class DynamicProxyDemo {


    public static void main(String[] args) {
        testJdkProxy();
        testCglibProxy();
        testDynamicProxyUtil();
    }

    public static void testJdkProxy() { // JDK代理
        DynamicProxyApi dynamicProxyApi = new DynamicProxyImpl();
        DynamicProxyApi proxyInstance = (DynamicProxyApi) Proxy.newProxyInstance(
            DynamicProxyImpl.class.getClassLoader(),
            new Class[]{DynamicProxyApi.class},
            new InvocationHandlerImpl(dynamicProxyApi));
        proxyInstance.method("xyz");
    }

    public static void testCglibProxy() { // cglib代理
        DynamicProxyApi dynamicProxyApi = new DynamicProxyImpl();
        DynamicProxyApi proxyInstance = (DynamicProxyApi) Enhancer.create(
            DynamicProxyApi.class, new MethodInterceptorImpl(dynamicProxyApi));
        proxyInstance.method("xyz");
    }

    public static void testDynamicProxyUtil() {
        DynamicProxyUtil dynamicProxyUtil = new DynamicProxyUtil();
        DynamicProxyBean dynamicProxyBean = (DynamicProxyBean) dynamicProxyUtil.getProxy(DynamicProxyBean.class);
        dynamicProxyBean.method("xyz");
    }


}
